/*
 * Copyright 2008 Nordic Consulting & Development Company
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.ncdc.web.login.client;

import java.util.Set;

import com.google.gwt.i18n.client.Dictionary;

/**
 * Handles configuration stored in site specific {@link Dictionary}.
 * <p>
 * Created on Apr 29, 2008
 *
 * @author hshsce
 * @version $Id: ConfigurationHandler.java 103 2008-06-02 14:44:57Z hshsce $
 */
public class ConfigurationHandler {

	private final Dictionary m_dictionary;

	private final Set<String> m_keys;

	/**
	 * Creates configuration handled for specified <code>dictionary</code>.
	 *
	 * @param dictionary
	 */
	public ConfigurationHandler(final String dictionary) {
		if (dictionary == null) {
			throw new IllegalArgumentException("dictionary cannot be null");
		}
		m_dictionary = Dictionary.getDictionary(dictionary);
		m_keys = m_dictionary.keySet();
	}

	/**
	 * Returns property of given <code>name</code>.
	 * If there is no such property defined, or it's value
	 * is set to <code>null</code>, the <code>defaultValue</code>
	 * is returned.
	 *
	 * @param name the name of property.
	 * @param defaultValue the default property value.
	 * @return the value of property.
	 */
	public String getProperty(final String name, final String defaultValue) {
		final String value;
		if (m_keys.contains(name)) {
			final String tmpValue = m_dictionary.get(name);
			if (tmpValue == null) {
				value = defaultValue;
			} else {
				value = tmpValue;
			}
		} else {
			value = defaultValue;
		}
		return value;
	}

}
